package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务人员认证审核表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {

    @Resource
    private WorkerCertificationMapper workerCertificationMapper;
    @Resource
    private WorkerCertificationAuditMapper workerCertificationAuditMapper;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> page(WorkerCertificationAuditPageQueryReqDTO dto) {
        //1.分页查询用户
        Page<WorkerCertificationAudit> page = PageUtils.parsePageQuery(dto, WorkerCertificationAudit.class);
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = Wrappers.<WorkerCertificationAudit>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(dto.getName()), WorkerCertificationAudit::getName, dto.getName())
                .like(ObjectUtil.isNotEmpty(dto.getIdCardNo()), WorkerCertificationAudit::getIdCardNo, dto.getIdCardNo())
                .eq(ObjectUtil.isNotEmpty(dto.getAuditStatus()) , WorkerCertificationAudit::getAuditStatus, dto.getAuditStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getCertificationStatus()), WorkerCertificationAudit::getCertificationStatus, dto.getCertificationStatus());
        Page<WorkerCertificationAudit> pages = workerCertificationAuditMapper.selectPage(page, queryWrapper);

        //3.封装数据
        return PageUtils.toPage(pages, WorkerCertificationAuditResDTO.class);
    }

    /**
     * 服务人员认证审核
     * @param id
     * @param rejectReason
     * @param certificationStatus
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(Long id, String rejectReason, Integer certificationStatus) {
        WorkerCertificationAudit audit = getById(id);
        if(ObjectUtil.isNull(audit)){
            throw new IllegalArgumentException("服务人员认证审核信息不存在");
        }
        audit.setAuditStatus(1);
        audit.setAuditorId(UserContext.currentUserId());
        audit.setAuditorName(UserContext.currentUser().getName());
        audit.setAuditTime(LocalDateTime.now());
        audit.setCertificationStatus(certificationStatus);
        WorkerCertification workerCertification = workerCertificationMapper.selectById(audit.getServeProviderId());
        workerCertification.setCertificationStatus(certificationStatus);
        workerCertificationMapper.updateById(workerCertification);
        if(StrUtil.isNotBlank(rejectReason)){
            audit.setRejectReason(rejectReason);
            // 需要删除(不好说要不要删除,也不好说是不是在这里删除)

            updateById(audit);
        }else{
            updateById(audit);
        }
    }
}
